package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    public void saveDishWithFlavor(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dish.setStatus(StatusConstant.ENABLE);
        dishMapper.insertOnlyDish(dish);
           Long dishid =dish.getId();
           List<DishFlavor> flavors= dishDTO.getFlavors();//这一步跟创建dish那一步类似的
           if(flavors!=null&&flavors.size()>0){
               flavors.forEach(dishflavor->{dishflavor.setDishId(dishid);});//遍历每一个菜品口味为他们绑定主键id，来自于dish
               dishFlavorMapper.insertOnlyFlavors(flavors);//插入口味数据
           }





    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<Dish> page=dishMapper.pageQuery(dishPageQueryDTO);
       return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
           Dish dish = dishMapper.selectById(id);


        }
          List<Setmeal> setmealids =setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealids!=null&&setmealids.size()>0){
            throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        dishMapper.deleteOnlyDishByIds(ids);
        dishFlavorMapper.deleteOnlyDishFlavorByIds(ids);


    }

    @Override
    public DishVO selectById(Long id) {
             Dish dishes =dishMapper.getById(id);
                   List<DishFlavor> dishFlavors=dishFlavorMapper.getFlavorById(id);
                   DishVO dishVO=new DishVO();
                   BeanUtils.copyProperties(dishes,dishVO);
                   dishVO.setFlavors(dishFlavors);
                   return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //修改菜品表基本信息
        dishMapper.update(dish);

        //删除原有的口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        //重新插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertOnlyFlavors(flavors);
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        dishMapper.startOrStop(status,id);
    }

    @Override
    public List<Dish> selectDishByCategoryId(Integer categoryId) {

              List<Dish> dishes  = dishMapper.selectDishByCategoryId(categoryId);
              return dishes;
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}



